﻿using System;

using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;

namespace ConsoleApplication1
{
    class Program
    {
        //static private HashMap map = new HashMap();

        
        Dictionary<string, List<FileBean>> dict = new Dictionary<string, List<FileBean>>();

        static void Main(string[] args)
        {
            //1 读取指定文件
            //
            readFolder("D:\\");

            Console.Write("按任意键退出...");
            Console.ReadKey(true);
        }

        static void processFile()
        { 
            //1.从缓存中读取文件信息

            //若不存在信息，需要向服务器查询
            //若存在，直接将数据取出来
        }

        static void readFolder(string folderPath)
        {
            List<FileBean> list = new List<FileBean>();

            //C#遍历指定文件夹中的所有文件 
            DirectoryInfo theFolder = new DirectoryInfo(folderPath);


            //遍历文件夹
            foreach (DirectoryInfo folder in theFolder.GetDirectories())
            {
                Console.WriteLine(folder.FullName);
                FileBean dir = new FileBean();
                dir.Type = "dir";
                dir.Name = folder.FullName;
                dir.CreateTime = folder.CreationTimeUtc.Ticks;

                list.Add(dir);
                
            }
                
            //遍历文件
            foreach (FileInfo file in theFolder.GetFiles())
            {
                Console.WriteLine(file.FullName);

                FileBean fileBean = new FileBean();
                fileBean.Type = "dir";
                fileBean.Name = file.FullName;
                fileBean.CreateTime = file.CreationTimeUtc.Ticks;
                fileBean.Length = file.Length;
                fileBean.UpdateTime = file.LastWriteTime.Ticks;


                getSha1HashFromFile(file.FullName);

                list.Add(fileBean);
            }
                
        }

        protected static string GetMD5HashFromFile(string fileName)
        {
            FileStream file = new FileStream(fileName, FileMode.Open);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }

            Console.WriteLine(sb);
            return sb.ToString();
        }

        protected static string getSha1HashFromFile(string fileName)
        {
            string sendCheckSum = string.Empty;
            using (FileStream stream = File.OpenRead(fileName))
            {
                SHA1Managed sha = new SHA1Managed();
                byte[] checksum = sha.ComputeHash(stream);
                sendCheckSum = BitConverter.ToString(checksum).Replace("-", string.Empty);

            }

            Console.WriteLine(sendCheckSum);
            return sendCheckSum;
        }
    }
}
